package org.lanqiao.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.GiveGradeBean;
import org.lanqiao.bean.StuCourse;
import org.lanqiao.myUtils.Myutils;
import org.lanqiao.service.FactoryService;

public class StudentGradeSevelet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取当前的处理
		int atsc=Myutils.getInt(request.getParameter("atsc"));
		
		if(atsc==1){
			String tc=request.getParameter("tc");
			request.getSession().setAttribute("tc", tc);
			String userName=(String) request.getSession().getAttribute("username");
			//学生选择老师课程
			List tgc=FactoryService.getUserInfoManageService().getStuCInfo(userName,tc);
			
			int thcAll=tgc.size();
			
			request.getSession().setAttribute("stuAllScore", thcAll);
			
			request.getSession().setAttribute("GiveGrade", tgc);
			
			request.getRequestDispatcher("logined/TeaLoginedInfo/teacherGiveGrade.jsp").forward(request, response);
			
		}else{
		//获取学生的成绩单
		String stuGrade=request.getParameter("allStuGrade");
		System.out.println(stuGrade);
		//把学生的信息进行分割
		String[]strAllG=stuGrade.split(":");
		//获取学生的信息
		List allStu=(List) request.getSession().getAttribute("GiveGrade");
		//学生成绩的集合
		List scList=new ArrayList<StuCourse>();

		int i=0;
		Iterator it=allStu.iterator();
		while(it.hasNext()){
			//获取list中学生名字
			GiveGradeBean ggb=(GiveGradeBean) it.next();
			//获取学生实例化
			StuCourse sc=(StuCourse) Myutils.getNewInstance(StuCourse.class);
			//吧学生的信息存入成绩单中
			sc.setUsername(ggb.getUsername());
			sc.setCourseid(ggb.getCourseid());
			sc.setCoursescore(Myutils.getInteger(strAllG[i++]));
			scList.add(sc);
		}
		boolean result=FactoryService.getUserInfoManageService().updateScore(scList);
		String userName=(String) request.getSession().getAttribute("username");
		String tc=(String) request.getSession().getAttribute("tc");
		List tgc=FactoryService.getUserInfoManageService().getStuCInfo(userName,tc);
		int thcAll=tgc.size();
		
		request.getSession().setAttribute("stuAllScore", thcAll);
		
		request.getSession().setAttribute("GiveGrade", tgc);
		
		request.getRequestDispatcher("logined/TeaLoginedInfo/teacherGiveGrade.jsp").forward(request, response);

		}
	}
	

}
